package com.ruoyi.lession.Scheduling;

import com.ruoyi.lession.entily.Lesson;
import com.ruoyi.lession.service.LessonService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@Component
public class EsScheduling {

    @Resource
    LessonService lessonService;

    @Resource
    RestHighLevelClient restHighLevelClient;

//    @Scheduled(fixedDelay = 500000)
    public void  TimerClassSchedule() throws IOException {
        List<Lesson> lesson=lessonService.findByService();
        for (Lesson lesson1 : lesson) {
            //如果状态1审核通过   0、审核未通过
            if(lesson1.getStatus()==1){
                IndexRequest indexRequest = new IndexRequest("eslesson", "eslesson");
                indexRequest.id(lesson1.getId()+"");
                HashMap<String, Object> map = new HashMap<>();
                map.put("name",lesson1.getName());
                map.put("video",lesson1.getVideo());
                map.put("note",lesson1.getNote());
                map.put("athour",lesson1.getAthuor());
                map.put("creattime",lesson1.getCreattime());
                map.put("endtime",lesson1.getEndtime());
                map.put("status",lesson1.getStatus());
                map.put("del",lesson1.getDel());
                indexRequest.source(map);
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            }
        }

        System.out.println("定时器增量同步！");
    }
}
